package com.acwebsitedesign.uRateDrugs.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

import com.acwebsitedesign.uRateDrugs.util.DbUtils;

@Component("DrugRepository")
public class DrugRepositoryImpl implements DrugRepository {
	private SimpleJdbcTemplate simpleJdbcTemplate = DbUtils
			.getSimpleJdbcTemplate();

	public List<Drug> getDrugByName(String name) {
		String sql = "SELECT d.id, d.name, d.classOfDrug, d.whyIsThisMedicationPrescribed, d.otherBrands, d.sideEffects, d.warnings, d.treats  FROM drug d where d.name=?";
		return simpleJdbcTemplate.query(sql, new DrugMapper(), name);
	}
	
	public List<Drug> getDrugByAlias(String name) {
		String sql = "SELECT d.id, d.name, d.classOfDrug, d.whyIsThisMedicationPrescribed, d.otherBrands, d.sideEffects, d.warnings, d.treats  FROM drug d, drug_alias da where da.drug_id=d.id and da.drug_alias=?";
		return simpleJdbcTemplate.query(sql, new DrugMapper(), name);
	}
	
	public List<Drug> getDrugLike(String name) {
		String sql = "SELECT d.id, d.name, d.classOfDrug, d.whyIsThisMedicationPrescribed, d.otherBrands, d.sideEffects, d.warnings, d.treats  FROM drug d where d.name LIKE ?";
		return simpleJdbcTemplate.query(sql, new DrugMapper(), "%" +name +"%");

	}

	public List<Drug> getDrugById(int id) {
		String sql = "SELECT d.id, d.name, d.classOfDrug, d.whyIsThisMedicationPrescribed, d.otherBrands, d.sideEffects, d.warnings, d.treats FROM drug d where id=?";
		return simpleJdbcTemplate.query(sql, new DrugMapper(), id);
	}

	public void saveDrug(Drug drug) {
		if (drug.getId() == 0) {
			insertDrug(drug);
		} else {
			updateDrug(drug);
		}
	}

	public void updateDrug(Drug drug) {
		String sql = "UPDATE drug SET name=?, classOfDrug=?, whyIsThisMedicationPrescribed=?, otherBrands=?, sideEffects=?, warnings=?, treats=? where id=?";
		simpleJdbcTemplate.update(sql, drug.getName(), drug.getClassOfDrug(),
				drug.getWhyIsThisMedicationPrescribed(), drug.getOtherBrands(),
				drug.getSideEffects(), drug.getWarnings(), drug.getTreats(),
				drug.getId());
	}

	public void insertDrug(Drug drug) {
		String sql = "INSERT INTO drug (name, classOfDrug, whyIsThisMedicationPrescribed, otherBrands, sideEffects, warnings, treats) VALUES (?,?,?,?,?,?,?)";
		simpleJdbcTemplate.update(sql, drug.getName(), drug.getClassOfDrug(),
				drug.getWhyIsThisMedicationPrescribed(), drug.getOtherBrands(),
				drug.getSideEffects(), drug.getWarnings(), drug.getTreats());
	}
	
	public void saveDrugAlias(Drug drug, String alias) {
		String sql = "insert into drug_alias (drug_alias, drug_id) values (?,?)";
		simpleJdbcTemplate.update(sql, alias, drug.getId());
	}

	public void removeDrugById(int id) {
		String sql = "DELETE FROM drug WHERE id=?";
		simpleJdbcTemplate.update(sql, id);
	}

	public void removeDrugByName(String name) {
		String sql = "DELETE FROM drug WHERE name=?";
		simpleJdbcTemplate.update(sql, name);
	}
	


	private static final class DrugMapper implements RowMapper<Drug> {
		public Drug mapRow(ResultSet rs, int rowNum) throws SQLException {
			int id = rs.getInt("d.id");
			String drugName = rs.getString("d.name");
			String classOfDrug = rs.getString("d.classOfDrug");
			String whyIsThisMedicationPrescribed = rs
					.getString("d.whyIsThisMedicationPrescribed");
			String otherBrands = rs.getString("d.otherBrands");
			String sideEffects = rs.getString("d.sideEffects");
			String warnings = rs.getString("d.warnings");
			String treats = rs.getString("d.treats");

			Drug drug = new Drug(id, drugName, classOfDrug,
					whyIsThisMedicationPrescribed, otherBrands, sideEffects,
					warnings, treats);
			return drug;
		}
	}
}
